<?php
// vim: sw=4:sts=4:expandtab

if ( isset($this->message) ) {
echo '<p class="messagebox info">' . $this->_($this->message) . '</p>';
}
?>
<!-- The main Map div, will be populated by OpenLayers in JavaScript -->
<div id="mainMap"></div>

<script type="text/javascript">
//<![CDATA[

// resize the mainMap according to the windowheight, if it is resized
$('#mainMap').addClass('width99');
$('#mainMap').addClass('is-processing');

// this function should be in a lib
function loadScript (scriptUrl) {
    var script  = document.createElement('script');
    script.type = 'text/javascript';
    script.src  = scriptUrl;
    $('head').append(script);
}

// this function should be in a lib
function loadStyle (styleUrl, media) {
    if(!media) {
        var media = 'screen';
    }
    var link  = document.createElement('link');
    link.type = 'text/css';
    link.rel = 'stylesheet';
    link.media = media;
    link.href  = styleUrl;
    $('head').append(link);
}

/**
 * Check if NewMapManager and OpenLayers objects exist,
 * if so execute goForMap function,
 * else start this function again after 100 ms
 */
function waitForMap () {
    if (typeof(NewMapManager) == 'undefined' || typeof(OpenLayers) == 'undefined' || typeof(G_HYBRID_MAP) == 'undefined') {
        setTimeout('waitForMap()', 100);
    } else {
        goForMap();
    }
}

loadStyle('<?php echo $this->componentUrlBase . 'css/OpenLayers.css'; ?>', 'screen');
loadScript('<?php echo 'http://maps.google.com/maps?file=api&v=2&hl=de&key=' . $this->config->apikey->google . '&async=2'; ?>');
//loadScript('<?php echo 'http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=' . $this->config->apikey->yahoo . ''; ?>');
loadScript('<?php echo $this->componentUrlBase . 'classes/OpenLayers.js'; ?>');
loadScript('<?php echo $this->componentUrlBase . 'classes/NewMapManager.js'; ?>');

$(document).ready(waitForMap);

function goForMap () {
    $('#mainMap').height( $('#mainMap').width() ); // TODO resize map to page height
    $('#mainMap').removeClass('is-processing');
    var manager             = new NewMapManager(
        $('#mainMap').get(0),                      // the dom object containing the map
        <?php echo json_encode($this->extent) ?>,       // the extend on the map, which is shown
        '<?php echo (string) $this->jsonRequestUrl ?>'  // the request URL, wher to get the markerdata
    );

    // the themePath doesn't seam to work
    manager.themePath       = '<?php echo $this->componentUrlBase ?>css/OpenLayers.css';
    manager.defaultLayer    = '<?php echo $this->config->default->layer ?>';

    // set the image path to the ontowiki theme's image folder
    manager.imgPath         = '<?php echo $this->themeUrlBase; ?>images/';

    manager.icon = new OpenLayers.Icon(
        '<?php echo $this->componentUrlBase . $this->config->icon->path; ?>',
        new OpenLayers.Size(<?php echo $this->config->icon->size; ?>),
        new OpenLayers.Pixel(<?php echo $this->config->icon->offset; ?>)
    );
    manager.clusterIcon = new OpenLayers.Icon(
        '<?php echo $this->componentUrlBase . $this->config->cluster->path; ?>',
        new OpenLayers.Size(<?php echo $this->config->cluster->size; ?>),
        new OpenLayers.Pixel(<?php echo $this->config->cluster->offset; ?>)
    );
    manager.prepare();
    manager.init();
}

//]]>
</script>
